package org.example.model;

import org.example.constant.BusinessEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("统一返回结果")
public class Result<T> implements Serializable {

    @ApiModelProperty("状态码")
    private Integer code = 200;

    @ApiModelProperty("消息")
    private String message = "ok";

    @ApiModelProperty("数据")
    private T data;

    public static <T> Result<T> success(T data){
        return new Result<>(200,"ok",data);
    }

    public static <T> Result<T> fail(Integer code,String message,T data){
        return new Result<>(code, message, data);
    }

    public static <T> Result<T> fail(BusinessEnum businessEnum){
        Result result = new Result();
        result.setCode(businessEnum.getCode());
        result.setMessage(businessEnum.getDesc());
        result.setData(null);
        return result;
    }


    public static Result<String> handle(Boolean flag) {
        if (  flag ){
            return Result.success("操作成功");
        }
        return Result.fail(BusinessEnum.OPERATION_FAIL);
    }

}
